package com.rq.app_base.use;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.base.rq.LOG;
import com.rq.app_base.CtrUI;

import java.lang.reflect.ParameterizedType;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

/**
 * @author RQ
 * date    2021-07-13 14:56
 */
public class BaseFragment<P extends BaseController> extends Fragment implements CtrUI<View> {

  protected P mPresenter;
  boolean isCreate = false;
  View fraRoot;
  private String fraTag;

  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPresenter = getController(this, 0);
    LOG.e("BaseFragment", getStrTag() + ".onCreate:");
  }

  @Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                           @Nullable Bundle savedInstanceState) {
    LOG.e("BaseFragment", getStrTag() + ".onCreateView");
    //        if (mPresenter != null) {
    //            Object pass1 = mPresenter.getPass(0);
    //            LOG.e("OnAction", "onCreateView " + getStrTag() + "  " + pass1);
    //        }
    if (fraRoot != null) {
      return fraRoot;
    }
    if (mPresenter != null) {
      this.fraRoot = inflater.inflate(mPresenter.getLayoutId(), null);
      mPresenter.setUi(this);
      mPresenter.onViewCreated(savedInstanceState);
      return fraRoot;
    }
    fraRoot = super.onCreateView(inflater, container, savedInstanceState);
    return fraRoot;
  }

  @Override
  public View getView(int id) {
    return this.fraRoot.findViewById(id);
  }

  @Override
  public boolean openPage(Class clazz) {
    return false;
  }

  @Override
  public void quit() {

  }

  public String getStrTag() {
    if (mPresenter != null) {
      if (fraTag == null) {
        fraTag = this.getClass().getSimpleName() + ":" + mPresenter.getClass()
                                                                   .getSimpleName() + "-" + System
            .currentTimeMillis();
      } else if (fraTag.indexOf("-") == fraTag.lastIndexOf("-")) {
        fraTag = fraTag.replace("-", "-" + mPresenter.getClass().getSimpleName() + "-");
      }
    } else if (fraTag == null) {
      fraTag = this.getClass().getSimpleName() + "-" + System.currentTimeMillis();
    }
    return fraTag;
  }

  //反射获取当前Presenter对象
  protected P getController(Object o, int i) {
    return getController((Class<P>) ((ParameterizedType) (o.getClass().getGenericSuperclass()))
        .getActualTypeArguments()[i]);
  }

  public P getController(Class<P> clazz) {
    try {
      return clazz.newInstance();
    } catch (java.lang.InstantiationException e) {
      LOG.showUserWhere();
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      LOG.showUserWhere();
      e.printStackTrace();
    } catch (ClassCastException e) {
      LOG.showUserWhere();
      e.printStackTrace();
    } catch (NullPointerException e) {
      LOG.showUserWhere();
      e.printStackTrace();
    }
    return null;
  }


  //反射获取当前Presenter对象
  public BaseController getController() {
    return mPresenter;
  }

}
